package nc.bs.lang.util;

import nc.bs.common.util.ObjectUtils;
import nc.bs.io.util.PropertiesUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author duanyp1
 * @date 2024/1/17 15:55
 */
public class NCDefLangUtil {

    private static final String default_properties = "defLang.properties";

    private static final Map<String, Properties> propertiesMap = new HashMap<>();


    public static String getMessage(String code, Object... args) {
        return getMessage(default_properties, code, args);
    }

    public static String getMessage(String fileCode, String code, Object... args) {
        return getMessage(NCDefLangUtil.class, fileCode, code, args);
    }

    public static String getMessage(Class<?> clazz, String fileCode, String code, Object... args) {
        boolean needUpdate = PropertiesUtil.needUpdate(clazz, fileCode);
        Properties properties;
        if(needUpdate){
            properties = PropertiesUtil.getProperties(clazz, fileCode);
        }else {
            properties = propertiesMap.computeIfAbsent(fileCode, (k) -> PropertiesUtil.getProperties(clazz, k));
        }
        String msg;
        return ((msg = properties.getProperty(code)) != null && !ObjectUtils.isEmpty(args)) ? String.format(msg, args) : msg;
    }

}
